//
//  MDGameListView.swift
//  麦兜直播
//
//  Created by yeqingyu on 16/10/16.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

private let GameListID = "MDGameCellID"
private let itemW = screenWidth/5.0
private let itemH:CGFloat = 80
import UIKit

class MDGameListView: UIView {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    
    var homeGroup:[MDHomeGroup]?{
        didSet{
            
            collectionView.reloadData()
        }
    }
    
    
  class func show() -> MDGameListView {
        
     return  Bundle.main.loadNibNamed("MDGameListView", owner: nil, options: nil)?.first as! MDGameListView
    }

    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 设置该控件不随着父控件的拉伸而拉伸
        autoresizingMask = UIViewAutoresizing()
        layout.itemSize = CGSize(width: itemW, height: itemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(UINib(nibName: "MDGameCell", bundle: nil), forCellWithReuseIdentifier: GameListID)
    }
    
}


extension MDGameListView:UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return homeGroup?.count ?? 0
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:MDGameCell = collectionView.dequeueReusableCell(withReuseIdentifier: GameListID, for: indexPath) as! MDGameCell
        cell.homeGroup = homeGroup?[indexPath.row]
        cell.line.isHidden = true
        return cell
    }
}
